import java.util.*;

public class Main9 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = Integer.parseInt(sc.nextLine());
        List<String> idCards = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            idCards.add(sc.nextLine());
        }

        while (sc.hasNextLine()) {
            String command = sc.nextLine();
            if (command.equals("sort1")) {
                sort1(idCards);
            } else if (command.equals("sort2")) {
                sort2(idCards);
            } else {
                System.out.println("exit");
                break;
            }
        }

        sc.close();
    }

    public static void sort1(List<String> idCards) {
        List<String> formattedDates = new ArrayList<>();
        for (String idCard : idCards) {
            String birthDate = extractBirthDate(idCard);
            formattedDates.add(birthDate);
        }
        Collections.sort(formattedDates);
        for (String date : formattedDates) {
            System.out.println(date);
        }
    }

    public static void sort2(List<String> idCards) {
        Collections.sort(idCards, new Comparator<String>() {
            public int compare(String id1, String id2) {
                String date1 = extractBirthDate(id1);
                String date2 = extractBirthDate(id2);
                return date1.compareTo(date2);
            }
        });
        for (String idCard : idCards) {
            System.out.println(idCard);
        }
    }

    public static String extractBirthDate(String idCard) {
        String year = idCard.substring(6, 10);
        String month = idCard.substring(10, 12);
        String day = idCard.substring(12, 14);
        return year + "-" + month + "-" + day;
    }
}
